package com.tr.rentcar.controller.goods;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.tr.rentcar.model.bean.CarAttr;
import com.tr.rentcar.model.bean.CarComment;
import com.tr.rentcar.model.bean.Cars;
import com.tr.rentcar.model.bean.Place;
import com.tr.rentcar.model.bean.PlaceStore;
import com.tr.rentcar.model.bean.User;
import com.tr.rentcar.model.bean.UserAddress;
import com.tr.rentcar.model.bean.UserDiscount;
import com.tr.rentcar.model.dao.CarAttrDao;
import com.tr.rentcar.model.dao.CarCommentDao;
import com.tr.rentcar.model.dao.CarInfoDao;
import com.tr.rentcar.model.dao.OrderServiceDao;
import com.tr.rentcar.model.dao.PlaceDao;
import com.tr.rentcar.model.dao.UserAddressDao;
import com.tr.rentcar.model.dao.UserDiscountDao;

@WebServlet("/goods/order")
public class GoodsOrderController extends HttpServlet {
	/**
	 * 车辆订单
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			//获取该车信息
			String id=req.getParameter("id");
			Cars cars=new CarInfoDao().getInfoById(id);
			req.setAttribute("cars", cars);
			//根据车辆id查询属性
			List<CarAttr> attrList=new CarAttrDao().getAllById(cars.getCid());
			req.setAttribute("attrList", attrList);
			//获取评价
			List<CarComment> commentList=new CarCommentDao().getAll(cars.getCid(),0, 5);
			req.setAttribute("commentList", commentList);
			Long commentCount=new CarCommentDao().getAllCount(cars.getCid());
			req.setAttribute("commentCount", commentCount);
			//获取用户信息
			User user=(User) req.getSession().getAttribute("userinfo");
			req.setAttribute("user", user);//获取用户积分
			//根据用户id获取常用联系人
			List<UserAddress> addressList=new UserAddressDao().findById(user.getUid());
			req.setAttribute("addressList", addressList);
			//根据用户ID获取优惠券
			List<UserDiscount> discountList=new UserDiscountDao().findById(user.getUid());
			req.setAttribute("discountList", discountList);
			//遍历服务
			req.setAttribute("serviceList", new OrderServiceDao().getAll());
			//获取所在地
			PlaceDao pd=new PlaceDao();
			List<Place> placeList=pd.getAll();
			req.setAttribute("place", placeList);
			//获取时间计算时间差
			String quday=req.getParameter("quday");
			String huanday=req.getParameter("huanday");
			int cha=calDays(quday,huanday);
			req.setAttribute("cha", cha);
			
			//从参数中获取门店所在地的ID
			String qupid=req.getParameter("qulocation");
			String huanpid=req.getParameter("huanlocation");
			List<PlaceStore> quList=pd.getAllByid(qupid);
			List<PlaceStore> huanList=pd.getAllByid(huanpid);
			req.setAttribute("qulist", quList);
			req.setAttribute("huanlist", huanList);
			req.setAttribute("quplacename", pd.getPlaceById(qupid).getPname());
			req.setAttribute("quplacestorename", pd.getPlaceStoreById(req.getParameter("qustore")).getPsName());
			req.setAttribute("huanplacename", pd.getPlaceById(huanpid).getPname());
			req.setAttribute("huanplacestorename", pd.getPlaceStoreById(req.getParameter("huanstore")).getPsName());
			
			req.getRequestDispatcher("/WEB-INF/jsp/goods_order.jsp").forward(req, resp);
		} catch (Exception e) {
			req.getRequestDispatcher("/WEB-INF/jsp/error.jsp").forward(req, resp);
			e.printStackTrace();
		}
	}
	/**
	 * 保存用车人信息
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			String type=req.getParameter("type");
			PrintWriter out=resp.getWriter();
			if("save".equalsIgnoreCase(type)){
				UserAddress ua=new UserAddress();
				ua.setUaName(req.getParameter("name"));
				ua.setUaIdentity(req.getParameter("identity"));
				ua.setUaTel(req.getParameter("tel"));
				ua.setUid(Integer.parseInt(req.getParameter("id")));
				if(new UserAddressDao().save(ua)){
					out.println("success");
				}else{
					out.println("fail");
				}
			}else{
				List<UserAddress> addressList=new UserAddressDao().findById(Integer.parseInt(req.getParameter("uid")));
				if(addressList.size()!=0){
					out.println(JSONArray.toJSONString(addressList));
				}
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 计算两各日期时间差
	 * @param quday
	 * @param huanday
	 * @return
	 * @throws ParseException
	 */
	private int calDays(String quday,String huanday) throws ParseException{
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate=format.parse(quday);
		Date endDate=format.parse(huanday);
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(startDate);
		int cnt = 0;
		while(calendar.getTime().compareTo(endDate)!=0){
			calendar.add(Calendar.DATE, 1);
			cnt++;
		}
		return cnt;
		
	}

}
